package org.puppet.game.map;

import java.util.ArrayList;

import org.puppet.game.element.Puppet;

import android.content.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Region.Op;
import android.graphics.drawable.Drawable;

public class SceneMap {
	public float x;
	public float y;
	public int width;
	public int height;
	
	protected ArrayList<MapBlock> blocks;
	protected Resources resources;
	protected Drawable background;
	
	protected Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    
	public SceneMap() {
		blocks=new ArrayList<MapBlock>();
	    mPaint.setStyle(Paint.Style.STROKE);
	    mPaint.setStrokeWidth(3);
	    mPaint.setColor(Color.RED);
	}
	public SceneMap(Resources resources) {
		this();
		setResources(resources);
	}
	
	public void setResources(Resources resources){
		this.resources=resources;
	}
	
	public void addBlock(MapBlock block){
		blocks.add(block);
		width= block.x+block.width>width?(int)block.x+block.width:width;
		height= block.y+block.height>height?(int)(block.y)+block.height:height;
	}
	
	public void draw(Canvas canvas){
		for (int i=0;i< blocks.size();i++ ){
			drawBlock(canvas,i);
		}
	}
	public void drawBlock(Canvas canvas , int blockZIndex){
		MapBlock block=blocks.get(blockZIndex);
		canvas.drawBitmap(block.getMapImage(), block.x-x, block.y-y, null);
		if (block.getPath()!=null) {
			canvas.drawPath(block.getPath(),mPaint);
		}
	}
	
	public boolean collision(Puppet puppet){
		for (MapBlock block : blocks){
			if (block.region.op(puppet.getCoreRect(), Op.INTERSECT)){
				return true;
			}
		}
		return false;
	}
	
	public void synchBlock(float dx,float dy){
		for (MapBlock block : blocks){
			block.path.offset(dx, dy);
		}
	}
	
	public void scrollBy(float dx,float dy){
		this.x=x-dx;
		this.y=y-dy;
		synchBlock(dx,dy);
	}
	public void scrollTo(float x,float y){
		scrollBy(this.x-x,this.y-y);
	}
	
	public int getBlockNumber(){
		return blocks.size();
	}
	

	
	public void setBackground(Drawable background){
		this.background=background;
	}
	public Drawable getBackground(){
		return background;
	}
}
